package commonClasses;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintStream;
import java.util.Scanner;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class Journal {
	public static void main(String[] args) throws Exception {

		
		FileReader ff = new FileReader(
				"J:\\BC3_IMT_Training\\annotations\\IMT_training_pmids.txt");

		
		BufferedReader fb = new BufferedReader(ff);
		String name = fb.readLine();

		
		PrintStream ps = new PrintStream(
				"J:\\BC3_IMT_Training\\Journal name and lines.txt");
		
		
		while (name != null) {
			
			File file = new File("J:\\BC3_IMT_Training\\txt\\" + name
					+ ".txt");
			Scanner scanner = new Scanner(file);
			
			int count = 0;
			
			while (scanner.hasNextLine()) {
				scanner.nextLine();
				count++;
			}
			
			
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();

			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse (new File("J:\\BC3_IMT_Training\\articles\\pubmed_xml\\"+name+".xml"));
            NodeList nTitle=doc.getElementsByTagName("Title");
            Element t=(Element) nTitle.item(0);
            String title=t.getTextContent();
            NodeList nList = doc.getElementsByTagName("AbstractText");
    		Element e=(Element) nList.item(0);
    		String abs=e.getTextContent();
    		String[] str=abs.split(" ");
    		String abst=str[0]+" "+str[1]+" "+str[2];
    		
            ps.println(name+"\t"+title+"\t"+count+"\t"+abst);
               
			name=fb.readLine();
		}
	}

}
